import sys
import os
sys.path.append('sklearn')  # 添加src目录到Python路径

from data.preprocessing import DataPreprocessor
from models.train import ModelTrainer
from models.pipeline_utils import train_pipeline_with_gridsearch, save_entire_pipeline

def main():
    print("=== 机器学习项目完整流程 ===")
    
    # 1. 数据预处理
    print("\n1. 数据预处理...")
    preprocessor = DataPreprocessor()
    X_train, X_test, y_train, y_test, iris = preprocessor.load_and_preprocess_iris()
    
    print(f"训练集大小: {X_train.shape}")
    print(f"测试集大小: {X_test.shape}")
    
    # 2. 模型训练和评估
    print("\n2. 模型训练和评估...")
    trainer = ModelTrainer()
    trainer.initialize_models()
    trainer.train_models(X_train, y_train)
    trainer.evaluate_models(X_train, y_train)
    
    # 3. 测试最佳模型
    print("\n3. 测试集评估...")
    best_model = trainer.best_model
    test_accuracy = best_model.score(X_test, y_test)
    print(f"测试集准确率: {test_accuracy:.4f}")
    
    # 4. 保存模型
    print("\n4. 保存模型...")
    trainer.save_best_model('sklearn/models/best_simple_model.pkl')
    
    # 5. 使用Pipeline（高级用法）
    print("\n5. 使用Pipeline进行高级训练...")
    best_pipeline = train_pipeline_with_gridsearch(X_train, y_train)
    save_entire_pipeline(best_pipeline, 'sklearn/models/best_pipeline.pkl')
    
    print("\n=== 流程完成 ===")

if __name__ == "__main__":
    # 创建必要的目录
    os.makedirs('models', exist_ok=True)
    os.makedirs('src/data', exist_ok=True)
    os.makedirs('src/models', exist_ok=True)
    
    main()
